﻿// Seven Puzzle Aizu - 0121.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <queue>
#include <unordered_set>
#include <unordered_map>

using namespace std;
//https://vjudge.csgrandeur.cn/problem/Aizu-0121
/*
这是一个7拼图游戏，由8个正方形卡片和一个特定的框架组成。每个卡片都有一个唯一的标号：0, 1, 2, ..., 7。
在框架中，可以将卡片垂直排列成2行4列。

开始玩7拼图时，首先将所有卡片放入框架中。在框架内部，只有编号为0的卡片可以与上下左右相邻的卡片位置互换。
例如，在图(a)的状态下，可以将编号为0的卡片与其右侧相邻的编号为7的卡片交换位置，变为图(b)的状态。
或者可以将图(a)的状态下，将编号为0的卡片与其下方相邻的编号为2的卡片交换位置，变为图(c)的状态。
在图(a)状态下，编号为0的卡片只与上下左右相邻的编号为7和2的卡片交换位置，不允许其他位置的交换。

游戏的目标是将卡片整齐地排列成图(d)的状态。您需要编写一个程序，接受初始状态作为输入，并输出将卡片整齐排列所需的最小步数。
假设从输入的卡片状态可以达到图(d)的状态。

输入数据以空格分隔的8个数字的形式给出。它们表示初始状态下的卡片排列顺序。
例如，图(a)的数字表示为0 7 3 4 2 5 1 6，
图(c)的数字表示为2 7 3 4 0 5 1 6。

输入
以上述格式给出多个拼图。请处理输入直到结束。给定的拼图数量不超过1000。

输出
对于每个拼图，请输出达到最终状态所需的最小步数，每行一个结果。

样例输入
0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0
样例输出
0
1
28
*/

unordered_map<string,int> ss;
string str;
//0  3  4  7   
// 12      56

struct STATE {
	string s;
	int idx,step;
}state;

void solve() {
	string tmp;
	for (int i = 0; i < str.size(); i++) {
		if (str[i] != ' ') {
			tmp.push_back(str[i]);
		}
	}

	cout << ss[tmp] << endl;
}

void init() {

	int currZero = 0;
	ss.clear();
	str = "01234567";

	queue<STATE > q;
	struct STATE state; state.s = str; state.idx = currZero; state.step = 0;
	q.push(state); ss.insert({ state.s,0 });

	while (!q.empty()) {
		struct STATE state = q.front(); 
		q.pop();

		if (0 == state.idx) {
			//替换右边下边
			swap(state.s[state.idx], state.s[state.idx + 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx += 1;
				q.push(in);  ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx + 1]);


			swap(state.s[state.idx], state.s[state.idx + 4]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx += 4;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx + 4]);

		}
		else if (3 == state.idx) {
			//替换左边下边
			swap(state.s[state.idx], state.s[state.idx - 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx -= 1;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx -1]);


			swap(state.s[state.idx], state.s[state.idx + 4]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx += 4;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx + 4]);
		}
		else if (4 == state.idx) {
			//替换上边右边
			swap(state.s[state.idx], state.s[state.idx + 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx += 1;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx + 1]);


			swap(state.s[state.idx], state.s[state.idx - 4]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx -= 4;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx - 4]);
		}
		else if (7 == state.idx) {
			//替换上边左边
			swap(state.s[state.idx], state.s[state.idx - 4]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx -= 4;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx - 4]);

			swap(state.s[state.idx], state.s[state.idx - 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx -= 1;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx - 1]);
		}
		else if (2 == state.idx || 1 == state.idx) {
			//替换左右下边
			swap(state.s[state.idx], state.s[state.idx + 4]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx += 4;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx + 4]);

			swap(state.s[state.idx], state.s[state.idx + 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx += 1;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx + 1]);

			swap(state.s[state.idx], state.s[state.idx - 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx -= 1;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx - 1]);
		}
		else if (5 == state.idx || 6 == state.idx) {
			//替换左右上边
			swap(state.s[state.idx], state.s[state.idx + 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx += 1;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx + 1]);

			swap(state.s[state.idx], state.s[state.idx - 1]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx -= 1;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx - 1]);

			swap(state.s[state.idx], state.s[state.idx - 4]);
			if (ss.count(state.s) == 0) {
				struct STATE in = state; in.step++; in.idx -= 4;
				q.push(in); ss.insert({ in.s, in.step });
			}
			swap(state.s[state.idx], state.s[state.idx - 4]);
		}

	}

}


int main()
{
	init();
	while (getline(cin, str)) {
		solve();
	}

	return 0;
}

 